<?php

namespace App\Http\Resources\Backend;

use App\Models\BookOrder;
use Illuminate\Http\Resources\Json\Resource;

/**
 * Class BookRuleResource
 * @package App\Http\Resources\Backend
 */
class BookRuleResource extends Resource
{
    /**
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'count' => $this->orders()->count(),
            'totalPayPrice' => $this->orders()->sum('pay_price'),
            'cost' => $this->prices()->sum('total_price'),
            'id' => $this->id,
            'school' => new SchoolResource($this->whenLoaded('school')),
            'foods' => FoodResource::collection($this->whenLoaded('foods')),
            'prices' => $this->whenLoaded('prices'),
            'eat_date' => $this->eat_date,
            'eat_type' => $this->eat_type,
            'school_id' => $this->school_id,
            'is_settled' => $this->is_settled,
            'is_cancel' => $this->is_cancel,
            'updated_at' => $this->updated_at ? $this->updated_at->format("Y-m-d H:i:s") : null
        ];
    }
}
